package com.cn.auth.controller.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cn.auth.common.BasePageData;
import com.cn.auth.common.DateUtil;
import com.cn.auth.common.UserUtils;
import com.cn.auth.common.WebResponseCode;
import com.cn.auth.po.biz.Order;
import com.cn.auth.po.sys.Branch;
import com.cn.auth.po.sys.User;
import com.cn.auth.service.biz.OrderService;
import com.cn.auth.service.sys.BranchService;
import com.cn.auth.service.sys.UserService;

/**
 * 订单操作Controller
 * 
 * @author Huang Jian
 *
 */
@Controller
@RequestMapping("/order")
public class OrderController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	OrderService orderService;
	@Resource
	UserService userService;
	@Resource
	BranchService branchService;

	/**
	 * 去订单首页
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goIndex")
	public ModelAndView goIndex(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("keyList",branchService.getKeyList());
		Map<Integer,String> statusList = new HashMap<Integer,String>();
		statusList.put(0, "新建");
		statusList.put(1, "已付款");
		statusList.put(2, "待付款");
		modelView.addObject("statusList", statusList);
		modelView.setViewName("views/order/index");
		return modelView;
	}

	/**
	 * 去订单添加界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goAdd")
	public ModelAndView goAdd(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("keyList", branchService.getKeyList());
		modelView.setViewName("views/order/add");
		return modelView;
	}

	/**
	 * 去编辑页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goEdit")
	public ModelAndView goEdit(Integer id, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("zx_data", orderService.getOrderById(id));
		// 获取部门列表
		Branch branch = new Branch();
		List<Branch> branchList = branchService.getBranchList(branch);
		modelView.addObject("branchList", branchList);

		modelView.setViewName("views/order/edit");
		return modelView;
	}

	/**
	 * 获取数据列表信息
	 * 
	 * @param branch
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("getList")
	@ResponseBody
	public BasePageData getList(Order order, HttpServletRequest request, HttpServletResponse response) {
		BasePageData data = new BasePageData();
		List<Order> list = orderService.getOrderList(order);
		Integer count = orderService.getOrderListCount(order);
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		data.setCount(count);
		data.setData(list);
		return data;
	}

	/**
	 * 增加订单
	 * 
	 * @param branch
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("insert")
	@ResponseBody
	public BasePageData insert(Order order, HttpServletRequest request, HttpServletResponse response) {
		BasePageData data = new BasePageData();
		order.setSeriNo(DateUtil.getStringAllDate());
		order.setCreated(DateUtil.getNow());
		User users = UserUtils.getLoginUser();
		order.setuId(null != users ? users.getId() : null);
		Integer ins = orderService.insertOrder(order);
		if (null != ins) {
			userService.insertLog(users.getUname() + "添加订单信息");
		}
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		return data;
	}

	/**
	 * 删除操作
	 * 
	 * @param id       字符串id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public BasePageData delete(String id, HttpServletRequest request, HttpServletResponse response) {
		BasePageData data = new BasePageData();
		String[] ids = id.split(",");
		Integer del = orderService.deleteOrderById(ids);
		User users = UserUtils.getLoginUser();
		if (null != del) {
			userService.insertLog(users.getUname() + "删除订单信息");
		}
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		return data;
	}

	/**
	 * 更新操作
	 * 
	 * @param branch
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public BasePageData update(Order order, HttpServletRequest request, HttpServletResponse response) {
		BasePageData data = new BasePageData();
		order.setUpdated(DateUtil.getNow());
		Integer upd = orderService.updateOrderById(order);
		User users = UserUtils.getLoginUser();
		if (null != upd) {
			userService.insertLog(users.getUname() + "修改订单信息");
		}
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		return data;
	}

	/**
	 * 跟据id获取订单
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("get")
	@ResponseBody
	public BasePageData get(Integer id, HttpServletRequest request, HttpServletResponse response) {
		BasePageData data = new BasePageData();
		Order order = orderService.getOrderById(id);
		data.setData(order);
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		return data;
	}

}
